import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, bytecodePlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
const publicDir = resolve('resources')
const envDir = resolve('build')

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin(), bytecodePlugin()]
  },
  preload: {
    plugins: [externalizeDepsPlugin(), bytecodePlugin()]
  },
  renderer: {
    publicDir,
    envDir,
    //环境变量前缀自定义，默认 VITE_
    envPrefix: 'RENDERER_',
    resolve: {
      alias: {
        '@renderer': resolve('src/renderer/src')
      }
    },
    server: {
      'proxy': {
        '/mohu': {
          target: 'https://img.moehu.org',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/mohu/, '')
        },
        '/getImages': {
          target: 'https://api.apiopen.top/api/getImages',
          changeOrigin: true
        },
        '/apihzBase': {
          target: 'https://cn.apihz.cn',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/apihzBase/, '')
        },
        '/mobeilImg': {
          target: 'http://service.picasso.adesk.com',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/mobeilImg/, '')
        },
        '/360img': {
          target: 'http://cdn.apc.360.cn/index.php',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/360img/, '')
        },
        '/mypexels': {
          target: 'https://api.pexels.com',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/mypexels/, '')
        },
        '/Bing-Wallpaper-Action': {
          target: 'https://raw.onmicrosoft.cn',
          changeOrigin: true
        },
        '/4kWallpaper': {
          target: 'http://api.yujn.cn',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/4kWallpaper/, '')
        },
        '/setu': {
          target: 'https://setu.yuban10703.xyz',
          changeOrigin: true
        },
        '/moeImg': {
          target: 'https://imgapi.lie.moe/random',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/moeImg/, '')
        }
      }
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          silenceDeprecations: ["legacy-js-api"],
          additionalData: "@use '@renderer/styles/mixin.scss';@use '@renderer/styles/main.scss' as *;",
        }
      }
    }
  }
})
